//
//  YXYStatusCell.swift
//  SinaWeibo
//
//  Created by 袁向阳 on 16/6/27.
//  Copyright © 2016年 YXY.cn. All rights reserved.
//

import UIKit
import SnapKit

class YXYStatusCell: UITableViewCell {
    
    // 供外界赋值
    var status: YXYStatus? {
        didSet {
            originalView.status = status
            footerView.status = status
            
            if status?.retweeted_status == nil {
                retweetView.hidden = true
                
                // 卸载toolBar的顶部约束
                bottomConst?.uninstall()
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConst = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
            }else {
                retweetView.hidden = false
                retweetView.status = status
                bottomConst?.uninstall()
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConst = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }
        }
    }
    
    /// 底部toolBar的顶部约束
    var bottomConst: Constraint?
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        backgroundColor = UIColor(colorLiteralRed: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1.0)
        
        setUpSubView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 添加子视图
    private func setUpSubView () {
        
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(footerView)
        
        addSubViewConstraint()
        
    }
    // MARK: - 添加子控件约束
    private func addSubViewConstraint () {
        originalView.snp_makeConstraints { (make) -> Void in
            make.width.left.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
        }
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.width.left.equalTo(contentView)
        }
        footerView.snp_makeConstraints { (make) -> Void in
            self.bottomConst = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(36)
            make.left.right.equalTo(contentView)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(footerView)
        }
    }
    // 原创微博视图
    lazy var originalView: YXYOriginalView = YXYOriginalView()
    // 转发微博视图
    lazy var retweetView: YXYRetweetView = YXYRetweetView()
    // 底部toolBar视图
    lazy var footerView: YXYFooterToolBar = YXYFooterToolBar()
}


